package cn.edu.shengda;

import javafx.scene.shape.Circle;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

  class TestFrame extends JFrame {

    private JPanel contentPane;
    private JTextField textFieldheight; //长
    private JTextField textFieldwidth; //宽
    private JTextField R; //半径
    private JTextField Zhouc; //周长
    private JTextField Mianj; //面积

    //定义变量，用来接收监听到是选择圆或者长方形
    String shape;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestFrame frame = new TestFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TestFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 798, 582);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JRadioButton rdbtnNewRadioButton = new JRadioButton("\u957F\u65B9\u5F62");

        //选择 长方形 监听事件
        rdbtnNewRadioButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                shape = rdbtnNewRadioButton.getText();
            }
        });
        rdbtnNewRadioButton.setFont(new Font("宋体", Font.PLAIN, 20));

        JRadioButton radioButton = new JRadioButton("\u5706");

        //选择 圆 监听事件
        radioButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                shape = radioButton.getText();
            }
        });
        radioButton.setFont(new Font("宋体", Font.PLAIN, 28));

        //实现单选
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton);
        group.add(rdbtnNewRadioButton);

        JLabel lblNewLabel = new JLabel("\u957F\uFF1A");
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 25));

        textFieldheight = new JTextField();
        textFieldheight.setFont(new Font("宋体", Font.PLAIN, 25));
        textFieldheight.setColumns(10);

        JLabel label = new JLabel("\u5BBD\uFF1A");
        label.setFont(new Font("宋体", Font.PLAIN, 25));

        textFieldwidth = new JTextField();
        textFieldwidth.setFont(new Font("宋体", Font.PLAIN, 25));
        textFieldwidth.setColumns(10);

        JLabel label_1 = new JLabel("\u534A\u5F84\uFF1A");
        label_1.setFont(new Font("宋体", Font.PLAIN, 25));

        R = new JTextField();
        R.setFont(new Font("宋体", Font.PLAIN, 25));
        R.setColumns(10);

        JButton btnNewButton = new JButton("\u8BA1   \u7B97");

        //计算按钮 监听事件
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                //选择长方形
                if("长方形".equals(shape)) {
                    String height = textFieldheight.getText();
                    String width =  textFieldwidth.getText();

                    if("".equals(height) || "".equals(width)) {
                        JOptionPane.showMessageDialog(null, "输入不能有空");
                        return;
                    }

                    ShapeArea z = new Rectangle(Double.valueOf(width),Double.valueOf(height));
                    Zhouc.setText(String.valueOf(z.getPerimeter()));
                    Mianj.setText(String.valueOf(z.getArea()));
                }

                //选择圆
                else if("圆".equals(shape)) {
                    String r = R.getText();

                    if("".equals(r)) {
                        JOptionPane.showMessageDialog(null, "输入不能有空");
                        return;
                    }
                    ShapeArea c = (ShapeArea) new Circle(Double.valueOf(r));
                    Zhouc.setText(String.valueOf(c.getPerimeter()));
                    Mianj.setText(String.valueOf(c.getArea()));
                }

                else {
                    JOptionPane.showMessageDialog(null, "请选择形状");
                }
            }
        });
        btnNewButton.setFont(new Font("宋体", Font.PLAIN, 28));

        JLabel label_2 = new JLabel("\u5468\u957F\uFF1A");
        label_2.setFont(new Font("宋体", Font.PLAIN, 25));

        JLabel label_3 = new JLabel("\u9762\u79EF\uFF1A");
        label_3.setFont(new Font("宋体", Font.PLAIN, 25));

        Zhouc = new JTextField();
        Zhouc.setFont(new Font("宋体", Font.PLAIN, 50));
        Zhouc.setColumns(5);

        Mianj = new JTextField();
        Mianj.setFont(new Font("宋体", Font.PLAIN, 50));
        Mianj.setColumns(5);
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addGap(126)
                                .addComponent(rdbtnNewRadioButton, GroupLayout.PREFERRED_SIZE, 131, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(ComponentPlacement.RELATED, 276, Short.MAX_VALUE)
                                .addComponent(radioButton, GroupLayout.PREFERRED_SIZE, 98, GroupLayout.PREFERRED_SIZE)
                                .addGap(139))
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addGap(111)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                                        .addComponent(label, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(lblNewLabel))
                                .addGap(25)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                                                        .addComponent(textFieldheight, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(textFieldwidth, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 361, GroupLayout.PREFERRED_SIZE))
                                                                .addGap(24))
                                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                                .addComponent(label_1)
                                                                .addPreferredGap(ComponentPlacement.RELATED)))
                                                .addComponent(R, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.RELATED))
                                        .addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
                                                .addGap(45)
                                                .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                                                        .addComponent(label_3, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(label_2, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE))
                                                .addGap(29)
                                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
                                                        .addComponent(Mianj)
                                                        .addComponent(Zhouc, GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE))))
                                .addGap(73))
        );
        gl_contentPane.setVerticalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addGap(32)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(rdbtnNewRadioButton)
                                        .addComponent(radioButton, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE))
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addGap(28)
                                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                                        .addComponent(textFieldheight, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                                .addGap(45)
                                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                                        .addComponent(label, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(textFieldwidth, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE))
                                                .addGap(55))
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addGap(59)
                                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                                        .addComponent(R, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(label_1, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                                                .addGap(105)))
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
                                .addGap(44)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                        .addComponent(label_2, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addComponent(Zhouc, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
                                                .addGap(32)
                                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                                        .addComponent(label_3, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(Mianj, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE))))
                                .addGap(41))
        );
        contentPane.setLayout(gl_contentPane);
    }
}